// 以下为官方示例
var prefix = "/sys/baseHint"
$().ready(function() {
	ProvinceBind();
	CityBindOther();
	VillageBindOther();
	validateRule();
});
$("#Province").change(function(){
    CityBind();
})
$("#City").change(function(){
    VillageBind();
})
$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function Bind(str){
    $("#ProviceBind").val(str);
}
function ProvinceBind(){
	var provinceCode = $("#provinceCode").val();
    $("#Province").html("");
    var str="<option> 请选择 </option>";
    $.ajax({
        url:"/getProvince",
        success:function(data){
            $.each(data.obj,function(i,item){
            	if(provinceCode == item.areaId){
            	str+="<option selected value="+item.areaId+">"+item.areaName+"</option>"
            	}else{
            	str+="<option value="+item.areaId+">"+item.areaName+"</option>"
            	}
            })
            $("#Province").append(str)
        },
        error:function(){alert("Error");}
    })
}

function CityBindOther(){
	    var cityCode = $("#cityCode").val();
	    $("#City").html("");
	    var str="<option> 请选择 </option>";
	    $.ajax({
	        url:"/getChild",
	        success:function(data){
	            $.each(data.obj,function(i,item){
	            	if(cityCode == item.areaId){
                	str+="<option selected value="+item.areaId+">"+item.areaName+"</option>"
                	}else{
                	str+="<option value="+item.areaId+">"+item.areaName+"</option>"
                	}
	            })
	            $("#City").append(str);
	        },
	        error:function(){alert("Error");}

	    });
	}
function CityBind(){
	    $("#City").html("");
	    var options=$("#Province option:selected");
	    var province=options.val();
	    if(province==""){
	        return;
	    }
	    $("City").html("");
	    var str="<option> 请选择 </option>";
	    $.ajax({
	        url:"/getChild",
	        data:{"parentId":province},
	        success:function(data){
	            $.each(data.obj,function(i,item){
	                str+="<option value="+item.areaId+">"+item.areaName+"</option>";
	            })
	            $("#City").append(str);
	        },
	        error:function(){alert("Error");}

	    });
	}
function VillageBindOther(){
	var villageCode = $("#villageCode").val();
    $("Village").html("");
    var str="<option> 请选择 </option>";
    $.ajax({
        url:"/getChild",
        success:function(data){
            $.each(data.obj,function(i,item){
            	if(villageCode == item.areaId){
            	str+="<option selected value="+item.areaId+">"+item.areaName+"</option>"
            	}else{
            	str+="<option value="+item.areaId+">"+item.areaName+"</option>"
            	}
            })
            $("#Village").append(str);
        },
        error:function(){alert("Error");}
    });
}
function VillageBind(){
    $("Village").html("");
    var options=$("#City option:selected");
    var city=options.val();
    if(city==""){
        return;
    }
    $("#Village").html("");
    var str="<option> 请选择 </option>";
    $.ajax({
        url:"/getChild",
        data:{"parentId":city},
        success:function(data){
            $.each(data.obj,function(i,item){
                str+="<option value="+item.areaId+">"+item.areaName+"</option>";
            })
            $("#Village").append(str);
        },
        error:function(){alert("Error");}
    });
}
function update() {
	var areaCode = $('#Province').val() + "," + $('#City').val() + "," + $('#Village').val();
	var province = $("#Province option:selected").text();
	var city = $("#City option:selected").text();
	var village = $("#Village option:selected").text();
	$('#areaCode').val(areaCode);
	$('#areaName').val(province + "," + city + "," + village);
	$.ajax({
		cache : true,
		type : "POST",
		url : prefix + "/updateBaseHint",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			alert("Connection error");
		},
		success : function(data) {
			if (data.res == 1) {
				parent.layer.msg(data.resMsg);
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			} else {
				layer.alert(data.resMsg)
			}
		}
	});
}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			flag : {
				required : true,
			}
		},
		messages : {
			flag : {
				required : icon + "请选择状态",
			}
		}
	})
}